package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述:90. 子集 II
 *
 * @author Madison You
 * @created 22:57
 */
public class Mid_90_SubsetsWithDup {
    public static void main(String[] args) {
        new Mid_90_SubsetsWithDup().subsetsWithDup(new int[]{1, 2, 2});
    }

    List<List<Integer>> list = new ArrayList<>();

    List<Integer> temp = new ArrayList<>();

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        boolean[] flag = new boolean[nums.length];
        back(nums, 0, flag);
        return list;
    }

    private void back(int[] nums, int index, boolean[] flag) {
        list.add(new ArrayList<>(temp));
        for (int i = index; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && !flag[i-1]) {
                continue;
            }
            flag[i] = true;
            temp.add(nums[i]);
            back(nums, i + 1, flag);
            flag[i] = false;
            temp.remove(temp.size() - 1);
        }
    }
}
